package com.elepro.shoppingmall.mapper;

import com.elepro.shoppingmall.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    //通过username查找用户信息
    @Select("select username, password, valid from user where username = #{username}")
    User getUserByUsername(String username);

    //通过username查找用户权限
    @Select("select authority from user where username = #{username}")
    List<User> getAuthorityByUsername(String username);

    //增加用户
    @Insert("insert user(username, password, valid, authority, email, userPicture, userAddr, userMoney) " +
    "values(#{username}, #{password}, #{valid}, #{authority}, #{email}, #{userPicture}, #{userAddr}, #{userMoney})")
    int insertUser(User user);

    //删除用户
    @Delete("delete from user where id = #{id}")
    int deleteUserById(long id);

    @Delete("delete from order where user_id = #{id}")
    int deleteOrderByUserId(long id);

    @Delete("delete from message where id = message_id and user_id = #{id}")
    int deleteMessageByUserId(long id);

    @Delete("delete from user_message where user_id = #{id}")
    int deleteUserMessageByUserId(long id);

    //修改用户
    @Update("update user set username = #{username}, password = #{password}, authority = #{authority}, email = #{email}, " +
            "userPicture = #{userPicture}, userAddr = #{userAddr}, userMoney = #{userMoney} " +
            "where id = #{id}")
    int updateUser(User user);

    //查询所有用户
    @Select("select * from user")
    List<User> getAllUser();

    @Select("select id from user where authority = 'ROLE_common'")
    List<Long> getAllCommonUser();

    @Select("select * from user where id = #{id}")
    User getUserById(long id);

    @Select("select * from user where username = #{username}")
    User getUserByName(String username);
}
